package com.hjimi.ski.utils;

import com.hjimi.ski.bean.InitDeviceBean;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created By 孙岩
 * on 2023/11/22
 */
public class InitUtil {
    public static InitDeviceBean.DataBean getInitDevice(String initJson){
        InitDeviceBean.DataBean bean = null;
        try {
            JSONObject obj = new JSONObject(initJson);
            if (obj.getInt("code") == 200) {
                JSONObject object = new JSONObject(obj.getString("data"));

                JSONObject turnConfigObj = new JSONObject(object.getString("turn_config"));
                InitDeviceBean.DataBean.TurnConfigBean turnConfigBean = new InitDeviceBean.DataBean.TurnConfigBean(
                        JsonParseUtil.getBoolean(turnConfigObj, "is_open"),
                        JsonParseUtil.getStr(turnConfigObj, "start_time"),
                        JsonParseUtil.getStr(turnConfigObj, "end_time"),
                        JsonParseUtil.getStr(turnConfigObj, "weeks")
                );
                JSONObject bgmConfigObj = new JSONObject(object.getString("bgm_config"));
                InitDeviceBean.DataBean.BgmConfigBean bgmConfigBean = new InitDeviceBean.DataBean.BgmConfigBean(
                        JsonParseUtil.getBoolean(bgmConfigObj, "is_open"),
                        JsonParseUtil.getInt(bgmConfigObj, "bgm_id"),
                        JsonParseUtil.getStr(bgmConfigObj, "bgm_url"),
                        JsonParseUtil.getBoolean(bgmConfigObj, "is_loop")
                );
                 bean = new InitDeviceBean.DataBean(
                        JsonParseUtil.getStr(object, "mac"),
                        JsonParseUtil.getStr(object, "sn"),
                        JsonParseUtil.getInt(object, "screen_id"),
                        JsonParseUtil.getInt(object, "size_id"),
                        JsonParseUtil.getInt(object, "screen_angle_id"),
                        JsonParseUtil.getInt(object, "buffer_id"),
                        JsonParseUtil.getInt(object, "camera_angle_id"),
                        JsonParseUtil.getInt(object, "is_camera"),
                        JsonParseUtil.getStr(object, "ip"),
                        JsonParseUtil.getInt(object, "is_carousel"),
                        JsonParseUtil.getInt(object, "carousel_seconds"),
                        JsonParseUtil.getInt(object, "is_gesture"),
                        JsonParseUtil.getInt(object, "device_id"),
                        JsonParseUtil.getInt(object, "device_info_id"),
                        JsonParseUtil.getStr(object, "phone"),
                        JsonParseUtil.getStr(object, "token"),
                        turnConfigBean,
                        bgmConfigBean
                );
                return bean;
            } else if (obj.getInt("code") == 202) {

            } else {
                ToastUtils.show(obj.getString("msg"));
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}
